package com.action;

import java.util.List;

import com.model.Admin;
import com.model.Role;
import com.service.AdminService;
import com.service.AdminServiceImpl;
import com.service.RoleService;
import com.service.RoleServiceImpl;
import com.util.ConstantUtil;


public class LoginAction extends DefaultAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2094410127862574130L;
	AdminService service ;
	RoleService rservice ;
	Admin model;
	String msg="";
	String code = "";
	String type="";
	String id="";
	String password = "";
	String newpassword = "";
	public LoginAction()
	{
		service = new AdminServiceImpl();
		rservice = new RoleServiceImpl();
	}
	public String ad()
	{
		return INPUT;
	}
	public String out()
	{
		setSession(ConstantUtil.ACCOUNTSESSIONNAME, null);
		setSession(ConstantUtil.ROLESESSIONNAME,null);
		return INPUT;
	}
	public String changedpass()
	{
		if(getSession(ConstantUtil.ACCOUNTSESSIONNAME)!=null)
		{
			model = (Admin)getSession(ConstantUtil.ACCOUNTSESSIONNAME);
		}
		else
		{
			return INPUT;
		}
		if(type.equals("update"))
		{
			if(!model.getPassword().equals(password))
			{
				msg = "旧密码输入有误！";
			}else
			{
				model.setPassword(newpassword);
				service.updateAdmin(model);
				msg = "密码修改成功！";
			}
		}
		return "passshow";
	}
	public String main()
	{
		boolean a = true;
		if(getSession(ConstantUtil.ACCOUNTSESSIONNAME)!=null)
		{
			model = (Admin)getSession(ConstantUtil.ACCOUNTSESSIONNAME);
			a = false;
		}
		else
		{
			if(model==null || model.getLoginName()==null || model.getLoginName().equals(""))
			{
				return INPUT;
			}
		}
		if(getSession(ConstantUtil.VERIFYCODESESSIONNAME)==null && a)
		{
			msg = "验证码错误！";
			return INPUT;
		}
		if(!getSession(ConstantUtil.VERIFYCODESESSIONNAME).toString().equalsIgnoreCase(code)&& a)
		{
			msg = "验证码错误！";
			return INPUT;
		}
		
		List<Admin> list = service.getAdminByLoginName(model);
		if(list==null || list.size()<=0)
		{
			msg = "用户名密码错误！";
			return INPUT;
		}
		else
		{
				boolean flag = false;
				Admin ad1 = null;
				for(Admin ad:list)
				{
					if(ad.getPassword().equals(model.getPassword()))
					{
						flag = true;
						ad1 = ad;
					}
				}
				if(!flag)
				{
					msg = "密码错误！";
					return INPUT;
				}
				setSession(ConstantUtil.ACCOUNTSESSIONNAME, ad1);
				Role role = new Role();
				role.setId(ad1.getRoleId());
				role = rservice.getRoleById(role);
				setSession(ConstantUtil.ROLESESSIONNAME,role);
			
		}
		return SUCCESS;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Admin getModel() {
		return model;
	}
	public void setModel(Admin model) {
		this.model = model;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNewpassword() {
		return newpassword;
	}
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
	
	
}
